<template>
    <div id="app">
        <div class="category clearfix">
            <category-item @click="goToProductPage(categoryItem)" :categoryItem="categoryItem"  v-for="(categoryItem, index) in categoryItems" :key="index">
            </category-item>
        </div>
        <tabs-bar :activeTab="1"></tabs-bar>
    </div>
</template>

<script>
import categoryItem from '@components/category-item'
import utils from '@utils'
import { listCategory } from './api'

export default {
    name: 'app',
    components: {
        categoryItem
    },
    mixins: [ utils.MixinBase ],
    data() {
        return {
            categoryItems: []
        }
    },
    mounted() {
        this.wechatInit().then(()=> {
            this.init();
            this.wechatConfigInit().then(()=> {
                var shareOptions = {
                    shareTitle: "坚冰商务",
                    shareDesc: "亲！还在等什么，快来看看！",
                    shareUrl: utils.createAuthLink("index.html"),
                    sharePic: ''
                }

                utils.shareHandler({
                    type: 'timeLine',
                    ...shareOptions
                })

                utils.shareHandler({
                    type: 'appMessage',
                    ...shareOptions
                })
            })
        });
    },
    methods: {
        init() {
            this.$indicator.open({
                spinnerType: 'fading-circle',
                text: '加载中...'
            });
            listCategory({}).then((categoryItems)=> {
                this.categoryItems = categoryItems;
                this.$indicator.close();
            })
        },
        goToProductPage(categoryItem) {
            if(categoryItem) {
                utils.goToPage("product.html?id="+categoryItem.id);
            } else {
                utils.goToPage("product.html");
            }
        }
    }
}
</script>

<style>
.category {
    border-top: 1px solid #efefef;
}
.category-item-img img {
    display: flex;
    width: 1.5rem;
    margin: 0 auto;
    border-radius: 50%;
}
.category-item-name {
    color: #666;
    display: block;
    height: 24px;
    line-height: 24px;
    text-align: center;
    overflow: hidden;
    width: 100%;
}
</style>
